package com.siam.package_rider.controller.admin;

import com.siam.package_common.constant.BasicResultCode;
import com.siam.package_common.constant.Quantity;
import com.siam.package_common.entity.BasicResult;
import com.siam.package_common.exception.StoneCustomerException;
import com.siam.package_common.model.valid_group.ValidGroupOfAudit;
import com.siam.package_common.model.valid_group.ValidGroupOfId;
import com.siam.package_merchant.entity.Shop;
import com.siam.package_merchant.model.param.ShopParam;
import com.siam.package_rider.entity.TbRider;
import com.siam.package_rider.model.param.TbRiderParam;
import com.siam.package_rider.service.TbRiderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * ---------------------------
 * 骑手表 (TbRiderController)         
 * ---------------------------
 * 作者：  高品
 * 时间：  2025-03-29 18:55:18
 * ---------------------------
 */
@RestController
@RequestMapping("/rest/admin/rider")
public class AdminRiderController {

	@Autowired
	private TbRiderService tbRiderService;

	/**
	 * 保存骑手表
	 * @param param
	 * @return
	 */	
	@PostMapping(value="/insert")
	public BasicResult insert(@RequestBody TbRiderParam param) {
		tbRiderService.insert(param);
		return BasicResult.success();
	}

    /**
     * 删除骑手表
     * @param id
     * @return
     */
	@PostMapping(value="/delete")
	public BasicResult delete(@RequestParam Long id) {
		tbRiderService.delete(id);
		return BasicResult.success();
	}

    /**
     * 批量删除骑手表
     * @param idList
     * @return
     */
	@PostMapping(value="/batchDelete")
	public BasicResult batchDelete(@RequestBody List<Long> idList) {
		tbRiderService.batchDelete(idList);
		return BasicResult.success();
	}

	/**
	 * 修改骑手表
	 * @param param
	 * @return
	 */
	@PostMapping(value="/update")
	public BasicResult update(@RequestBody TbRiderParam param) {
		tbRiderService.updateByAdmin(param);
		return BasicResult.success();
	}

    /**
     * 基础分页查询
     * @param param
     * @return
     */    
	@PostMapping(value="/page")
	public BasicResult page(@RequestBody TbRiderParam param) {
		return BasicResult.success(tbRiderService.page(param));
	}
	
    /**
     * 根据主键查询
     * @param id
     * @return
     */ 	
	@PostMapping(value="/findById")
	public BasicResult findById(@RequestParam Long id) {
		return BasicResult.success(tbRiderService.findById(id));
	}

	/**
	 * 审核申请注册骑手信息
	 *
	 * @return
	 * @author 高品
	 */
	@PostMapping(value = "/auditApplySettled")
	public BasicResult auditApplySettled(@RequestBody @Validated(value = {ValidGroupOfId.class, ValidGroupOfAudit.class}) TbRiderParam riderParam){
		BasicResult basicResult = new BasicResult();

		if(riderParam.getStatus() == Quantity.INT_2 && StringUtils.isBlank(riderParam.getOpinion())){
			throw new StoneCustomerException("审核不通过时，审核意见不能为空");
		}

		if(riderParam.getStatus() == Quantity.INT_1){
			//审核通过
			//修改门店信息
			TbRider updateRider= new TbRider();
			updateRider.setId(riderParam.getId());
			updateRider.setAuditStatus(Quantity.INT_2);
			updateRider.setAuditTime(new Date());
			tbRiderService.updateById(updateRider);

			//TODO-发消息通知用户
		}else if(riderParam.getStatus() == Quantity.INT_2){
			//审核不通过
			//修改门店信息
			TbRider updateRider= new TbRider();
			updateRider.setId(riderParam.getId());
			updateRider.setAuditStatus(Quantity.INT_3);
			updateRider.setAuditReason(riderParam.getOpinion());
			updateRider.setAuditTime(new Date());
			tbRiderService.updateById(updateRider);

			//TODO-发消息通知用户
		}

		basicResult.setSuccess(true);
		basicResult.setCode(BasicResultCode.SUCCESS);
		basicResult.setMessage("审核成功");
		return basicResult;
	}
}